package dao;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import util.IDGenerator;

import bo.PersistentObject;
import bo.Producto;
import bo.Stock;


public class ProductoDAO extends AbstractDAO {
	
	public List<PersistentObject> findAll() {
		return super.findAll(Producto.class);
	}

	public Producto findById(String id) {
		return (Producto) super.findById(Producto.class, id);
	}

	@Override
	public void setChildIds(PersistentObject producto) {
		List<Stock> stocks = ((Producto)producto).getStocks();
		if (stocks != null) {
			for (Iterator iterator = stocks.iterator(); iterator.hasNext();) {
				Stock stock = (Stock) iterator.next();
				stock.setId(IDGenerator.generate());
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public void beforeUpdate(PersistentObject entity) {
		setChildIds(entity);
	}

	@Override
	public void beforeSave(PersistentObject entity) {
	}
}
